<?php 
namespace app\index\controller;

use app\common\model\CheckworkLeaveMsg;
use app\common\model\CompanyNotice;
use app\common\model\CompanyNoticeRead;

class Infomation extends Base
{
	/**
	 * 获取消息记录
	 */
	public function index()
	{
		//请假消息通知
		$leave_num = CheckworkLeaveMsg::where(['staff_id' => $this->staff_id,'status' => 0])->count();
		//项目通知
		$project_num = 0;
		//公司公告
		$notice_num = 0;
		//组装数组
		$res = [
				'leave_num' => $leave_num,
				'project_num' => $project_num,
				'notice_num' => $notice_num
		];
		return $this->success($res);
	}
	/**
	 * 员工请假消息通知
	 */
	public function leaveInfo()
	{
		$list = CheckworkLeaveMsg::where(['staff_id' => $this->staff_id])->order('id desc')->paginate()->toArray();
		//修改状态
		CheckworkLeaveMsg::where(['staff_id' => $this->staff_id])->update(['status' => 1]);
		return $this->success($list);
	}
	
	/**
	 * 公司公告列表
	 */
	public function notices()
	{
		$list = CompanyNotice::where(['state' => 1])->order('id desc')->paginate()->toArray();
		foreach ($list['data'] as &$rows) {
			$reader = CompanyNoticeRead::where(['notice_id' => $rows['id']])->value('reader');
			$reader = json_decode($reader,true);
			$rows['read_state'] = in_array($this->staff_id, $reader) ? 1 : 0;
		}
		return $this->success($list);
	}
	
	/**
	 * 标记已读
	 */
	public function readNotice()
	{
		$id = $this->request->param('id');
		$reader = CompanyNoticeRead::where(['notice_id' => $id])->value('reader');
		$reader = json_decode($reader,true);
		$reader[] = $this->staff_id;
		CompanyNoticeRead::where(['notice_id' => $id])->update(['reader' => json_encode($reader)]);
	}
}